package util
{
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	
	import mx.collections.ArrayList;
	
	import spark.components.DataGrid;

	public class ExportExcelOp
	{
		public function convertDGToHTMLTable(dg:DataGrid):String {
			
			//Set default values
			var font:String = dg.getStyle('fontFamily');
			var size:String = dg.getStyle('fontSize');
			var str:String = '';
			var colors:String = '';
			var style:String = 'style="font-family:'+font+';font-size:'+size+'pt;"';                
			var hcolor:Array;
			
			//Retrieve the headercolor
			if(dg.getStyle("headerColor") != undefined) {
				hcolor = [dg.getStyle("headerColor")];
			} else {
				hcolor = ["13421772"];
			}                
			
			//Set the htmltabel based upon knowlegde from the datagrid
			str+= '<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head><table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'">';
			//解决乱码<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head>
			//Set the tableheader data (retrieves information from the datagrid header              
			var theColumns:ArrayList = dg.columns as ArrayList;
			for(var i:int = 0;i<theColumns.length;i++) {
				//colors = dg.getStyle("themeColor");
				var columnObj:Object = theColumns.source[i];
				if(columnObj.headerText != undefined) {
					str+="<th "+style+">"+columnObj.headerText+"</th>";
					//trace(dg.columns[i].headerText);
					
				} else {
					str+= "<th "+style+">"+columnObj.dataField+"</th>";
					// trace(dg.columns[i].dataField);
				}
			}
			str += "</tr></thead><tbody>";
			colors = dg.getStyle("alternatingRowColors");
			
			//Loop through the records in the dataprovider and 
			//insert the column information into the table
			for(var j:int =0;j<dg.dataProvider.length;j++) {                    
				str+='<tr width="'+Math.ceil(dg.width)+'"align="center">';
				
				for(var k:int=0; k < dg.columns.length; k++) {
					
					//Do we still have a valid item?                        
					if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) {
						
						//Check to see if the user specified a labelfunction which we must
						//use instead of the dataField
						var columnObj:Object = theColumns.source[k];
						if(columnObj.labelFunction != undefined) {
							str += "<td width=\""+Math.ceil(columnObj.width)+"\" "+style+">"+columnObj.labelFunction(dg.dataProvider.getItemAt(j),columnObj)+"</td>";
							// trace(dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField));  
						} else {
							//Our dataprovider contains the real data
							//We need the column information (dataField)
							//to specify which key to use.
							str += "<td width=\""+Math.ceil(columnObj.width)+"\" "+style+">"+dg.dataProvider.getItemAt(j)[columnObj.dataField]+"</td>";
							// trace(dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]);
						}
					}
				}
				str += "</tr>";
			}
			str+="</tbody></table>";
			return str;
		}
		public  function loadDGInExcel(dg:DataGrid,url:String):void {
			
			if(dg==null || dg.columns==null)
			{
				return;
			}
			//Pass the htmltable in a variable so that it can be delivered
			//to the backend script
			
			var variables:URLVariables = new URLVariables(); 
			variables.htmltable    = convertDGToHTMLTable(dg);
			
			//Setup a new request and make sure that we are 
			//sending the data through a post
			var u:URLRequest = new URLRequest(url);
			u.data = variables; //Pass the variables
			u.method = URLRequestMethod.POST; //Don't forget that we need to send as POST
			
			
			//Navigate to the script
			//We can use _self here, since the script will through a filedownload header
			//which results in offering a download to the user (and still remaining in you Flex app.)
			navigateToURL(u,"_blank");
		}
	}
	/*
	protected function btnExcel_clickHandler(event:MouseEvent):void   {
	var ExcelModel :GetExcel = new GetExcel(); ExcelModel.loadDGInExcel(GV_Score,"http://123.aspx");
	}

	
	<%@ Page Language="C#" validateRequest="false"%>
	<%
	Response.AddHeader("Content-disposition", "attachment; filename=ExamScore.xls");
	Response.Write(Request.Form["htmltable"]);
	%>
	
	*/
}